package com.tcoiss.webservice.controller.webapi;


import com.tcoiss.common.core.domain.R;
import com.tcoiss.common.core.utils.StringUtils;
import com.tcoiss.common.core.web.domain.AjaxResult;
import com.tcoiss.common.log.annotation.ApiLog;
import com.tcoiss.common.log.annotation.Log;
import com.tcoiss.common.log.enums.BusinessType;
import com.tcoiss.common.component.api.model.ApiParam;
import com.tcoiss.common.component.api.model.K3Result;
import com.tcoiss.webservice.apiServer.HttpAPIServer;
import com.tcoiss.webservice.apiServer.InvokeContext;
import com.tcoiss.webservice.service.IApiService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 通用API调用接口
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/apiService" )
public class ApiServiceController {

    /*@Autowired
    private IGaodeApiService iGaodeApiService;*/

    @Autowired
    private HttpAPIServer httpAPIServer;

    @Autowired
    private IApiService iApiService;


    /*@Log(title = "查询行政区划" , businessType = BusinessType.EXPORT)
    @GetMapping("/queryAdminByLocations")
    public AjaxResult queryAdminByLocations(String location) {
        return AjaxResult.success(iGaodeApiService.getDistrictsIdByNumber(location));
    }
    @ApiLog(title = "查询地址列表" , businessType = BusinessType.EXPORT)
    @GetMapping("/queryLocations")
    public AjaxResult queryLocations(AddressKeyVo vo) {
        return iGaodeApiService.queryLocations(vo);
    }*/

    //@Log(title = "API调用" , businessType = BusinessType.EXECUTE_API)
    @PostMapping("/executeApi")
    public R<Map<String, Object>> executeApi(@RequestBody ApiParam apiParam){
        if(!StringUtils.isNotEmpty(apiParam.getApiCode())){
            return R.fail("webApi编码不能为空");
        }
        Map<String,Object> result = iApiService.executeByApiCode(apiParam.getApiCode(),apiParam.getParam());
        if(result==null){
            return R.fail("webApi接口请求失败");
        }
        return R.ok(result);
    }

    //@Log(title = "金碟API调用" , businessType = BusinessType.EXECUTE_KD_API)
    @PostMapping("/executeKdApi")
    public R<Map<String, Object>> executeKdApi(@RequestBody ApiParam apiParam){
        if(!StringUtils.isNotEmpty(apiParam.getApiCode())){
            return R.fail("webApi编码不能为空");
        }
        Map<String,Object> result = iApiService.executeKdByApiCode(apiParam.getApiCode(),apiParam.getParam());
        if(result==null){
            return R.fail("webApi接口请求失败");
        }
        return R.ok(result);
    }

    //@Log(title = "金碟k3API调用" , businessType = BusinessType.EXECUTE_KD_API)
    @PostMapping("/executeK3Api")
    public R<K3Result> executeK3Api(@RequestBody ApiParam apiParam){
        if(!StringUtils.isNotEmpty(apiParam.getApiCode())){
            return R.fail("webApi编码不能为空");
        }
        K3Result result = iApiService.executeK3Cloud(apiParam.getApiCode(),apiParam.getParam());
        if(result==null){
            return R.fail("webApi接口请求失败");
        }
        return R.ok(result);
    }

}
